package model.service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import dom.domain.Examplace;
import dom.domain.ExamplaceDAO;

public class EPService {
	private ExamplaceDAO epDao;
	private Map<String, String> map;

	public void setEpDao(ExamplaceDAO epDao) {
		this.epDao = epDao;
	}

	public String add(String epInfo) {
		map = new HashMap<String, String>();
		try {

			JSONObject jepInfo = JSONObject.fromObject(epInfo);
//			System.out.println(jepInfo.toString());
			if (!jepInfo.containsKey("epname")) {
				map.put("flag", "false");
				map.put("Info", "epname为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("epAddress")) {
				map.put("flag", "false");
				map.put("Info", "epAddress为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("epClassNo")) {
				map.put("flag", "false");
				map.put("Info", "epClassNo为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("epLimitNo")) {
				map.put("flag", "false");
				map.put("Info", "epLimitNo为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("provinceId")) {
				map.put("flag", "false");
				map.put("Info", "provinceId为空");
				return JSONObject.fromObject(map).toString();
			}
			Examplace temp = new Examplace();
			temp.setAllocatedNo(0);
			long count = epDao.getCount();
			temp.setEpId((int)count+1);
			temp.setEpAddress(jepInfo.getString("epAddress"));
			temp.setEpClassNo(Integer.parseInt(jepInfo
					.getString("epClassNo")));
			temp.setEpname(jepInfo.getString("epname"));
			temp
					.setLimitNo(Integer.parseInt(jepInfo.getString("epLimitNo")));
			temp.setProvinceId(Integer.parseInt(jepInfo
					.getString("provinceId")));
			epDao.save(temp);
			map.put("flag", "true");
			map.put("Info", "添加成功");

		} catch (JSONException e) {
			map.put("flag", "false");
			map.put("Info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}

	public String getList(String pageNum) {
		map = new HashMap<String, String>();
		try {
			int pageNo = Integer.parseInt(pageNum);
			
			List list = epDao.getList(pageNo, 10);
			JSONArray array = new JSONArray();
			for (int i = 0; i < list.size(); i++) {
				Examplace ep = (Examplace) list.get(i);
				JSONObject jInfo = JSONObject.fromObject(ep.toString());
				jInfo.put("province", constant.constant.province.get(ep.getProvinceId()));
				array.add(jInfo.toString());
			}
			if (array.size() == 0) {
				map.put("flag", "false");
				map.put("Info", "未添加任何数据");
			} else {
				map.put("flag", "true");
				map.put("Info", "获取成功");
				map.put("array", array.toString());
			}
		} catch (NumberFormatException e) {
			map.put("flag", "false");
			map.put("Info", "pageNum不是数字");
		}
		return JSONObject.fromObject(map).toString();
	}
	public String query(String epname)
	{
		map = new HashMap<String,String>();
		List list = epDao.query(epname);
		if(list.size()==0)
		{
			map.put("flag","false");
			map.put("Info", "未查到任何数据");
			return JSONObject.fromObject(map).toString();
		}
		JSONArray array = new JSONArray();
		for(int i=0;i<list.size();i++)
		{
			
			Examplace ep = (Examplace)list.get(i);
			JSONObject jInfo = JSONObject.fromObject(ep.toString());
			jInfo.put("province", constant.constant.province.get(ep.getProvinceId()));
			array.add(jInfo);
		}
		
		map.put("flag", "true");
		map.put("Info", "success");
		map.put("array", array.toString());
		return JSONObject.fromObject(map).toString();
	}
	public String getProvince()
	{
		map  = new HashMap<String,String>();
		JSONArray array = new JSONArray();
		Iterator iterator = constant.constant.province.keySet().iterator();
		while(iterator.hasNext()) {
			JSONObject jInfo = new JSONObject();
			int pid = (Integer) iterator.next();
			jInfo.put("pid", pid);
			jInfo.put("pname", constant.constant.province.get(pid));
			array.add(jInfo);
			}
		map.put("flag", "true");
		map.put("Info", "success");
		map.put("array", array.toString());
		return JSONObject.fromObject(map).toString();
	}
	public String deleteAll(String Info)
	{
		String epIds[]=Info.split(",");
		StringBuilder result = new StringBuilder();
		for(String s:epIds)
		{
			JSONObject jInfo = JSONObject.fromObject(delete(s));
			if(jInfo.getString("flag").equals("false"))
			{
				result.append(jInfo.getString("Info"));
			}
		}
		map.put("flag","true");
		map.put("Info",result.toString());
		return JSONObject.fromObject(map).toString();
	}

	public String delete(String epId) {
		map = new HashMap<String, String>();
		if (epId == null) {
			map.put("flag", "false");
			map.put("Info", "考点号为空");
		} else {
//			System.out.println(epId);
			Examplace temp = epDao.findById(Integer.parseInt(epId));
			if (temp == null) {
				map.put("flag", "false");
				map.put("Info", "考场号不存在");
			} else {
				epDao.delete(temp);
				map.put("flag", "true");
				map.put("Info", "删除成功");
			}
		}
		return JSONObject.fromObject(map).toString();
	}

	public String query(int epId) {
		map = new HashMap<String, String>();
		Examplace temp = epDao.findById(epId);
		if (temp == null) {
			map.put("flag", "false");
			map.put("Info", "考场号不存在");
		} else {
			// epDao.delete(temp);
			map.put("epInfo", JSONObject.fromObject(temp.toString()).toString());
			map.put("flag", "true");
			map.put("Info", "删除成功");
		}
		return JSONObject.fromObject(map).toString();
	}

	public String modify(String epInfo) {

		map = new HashMap<String, String>();
		try {

			JSONObject jepInfo = JSONObject.fromObject(epInfo);
			if (!jepInfo.containsKey("epId")) {
				map.put("flag", "false");
				map.put("Info", "epId为空");
				return JSONObject.fromObject(map).toString();
			}
			Examplace temp = epDao.findById(Integer.parseInt(jepInfo
					.getString("epId")));
			if (temp== null) {
				map.put("flag", "false");
				map.put("Info", "epId不存在");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("epname")) {
				map.put("flag", "false");
				map.put("Info", "epname为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("epAddress")) {
				map.put("flag", "false");
				map.put("Info", "epAddress为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("epClassNo")) {
				map.put("flag", "false");
				map.put("Info", "epClassNo为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("epLimitNo")) {
				map.put("flag", "false");
				map.put("Info", "epClassNo为空");
				return JSONObject.fromObject(map).toString();
			}
			if (!jepInfo.containsKey("provinceId")) {
				map.put("flag", "false");
				map.put("Info", "provinceId为空");
				return JSONObject.fromObject(map).toString();
			}
			
			temp.setEpAddress(jepInfo.getString("epAddress"));
			temp.setEpClassNo(Integer.parseInt(jepInfo
					.getString("epClassNo")));
			temp.setEpname(jepInfo.getString("epname"));
			temp
					.setLimitNo(Integer.parseInt(jepInfo.getString("epLimitNo")));
			temp.setProvinceId(Integer.parseInt(jepInfo
					.getString("provinceId")));
			epDao.attachDirty(temp);
			map.put("flag", "true");
			map.put("Info", "添加成功");

		} catch (JSONException e) {
			map.put("flag", "false");
			map.put("Info", e.getMessage());
		}
		return JSONObject.fromObject(map).toString();
	}
	

}